﻿using System.Collections.ObjectModel;
using System.Linq;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.SkiaSharpView.Painting.Effects;
using MeterReader.Common.Config;
using Prism.Mvvm;
using SkiaSharp;

namespace MeterReader.Models
{
    public partial class MeterDataCollection : BindableBase
    {
        private int _maxCount = GlobalSettings.MaxMaxSeriesPonts;

        /// <summary>
        /// 电能表测量值集合
        /// </summary>
        public ObservableCollection<MeterData> Collection { get; set; }

        /// <summary>
        /// 电能表最新的测量值
        /// </summary>
        public MeterData MeterLatestData => Collection!.LastOrDefault()!;

        /// <summary>
        /// 曲线绑定值
        /// </summary>
        public ObservableCollection<ISeries> Series { get; set; }

        /// <summary>
        /// 曲线 X 轴
        /// </summary>
        public Axis[] XAxes { get; set; } =
            new Axis[]
            {
                new Axis
                {
                    NameTextSize = 15,
                    NamePaint = new SolidColorPaint(SKColors.WhiteSmoke),
                    LabelsPaint = new SolidColorPaint(SKColors.DarkGray),
                    TextSize = 15,
                    SeparatorsPaint = new SolidColorPaint(SKColors.LightSlateGray) { StrokeThickness = 2 },
                    MinStep = 1,
                }
            };

        /// <summary>
        /// 曲线 Y 轴
        /// </summary>
        public Axis[] YAxes { get; set; } =
            new Axis[]
            {
                new Axis
                {
                    NameTextSize = 15,
                    NamePaint = new SolidColorPaint(SKColors.WhiteSmoke),
                    LabelsPaint = new SolidColorPaint(SKColors.DarkGray),
                    TextSize = 15,
                    SeparatorsPaint = new SolidColorPaint(SKColors.LightSlateGray)
                    {
                        StrokeThickness = 2,
                        PathEffect = new DashEffect(new float[] { 3, 3 })
                    },
                }
            };

        /// <summary>
        /// 集合添加方法
        /// </summary>
        public void AddToCollection(MeterData meterData)
        {
            Collection!.Add(meterData);
            if (Collection.Count > _maxCount)
            {
                Collection.RemoveAt(0);
            }
        }

        public MeterDataCollection()
        {
            Collection = new();
            Collection.CollectionChanged += Meters_CollectionChanged;
            // 曲线定义
            Series = new()
            {
                new LineSeries<MeterData>
                {
                    Values = Collection,
                    Fill = null,
                    GeometrySize = 0,
                    LineSmoothness = 1,
                    Mapping = (Collection, index) => new(index, Collection.MeterValue!.Value),
                    //IsHoverable = false,
                    //EasingFunction = null,
                }
            };
            LiveCharts.Configure(config => config.HasGlobalSKTypeface(SKFontManager.Default.MatchCharacter('汉')));
        }

        private void Meters_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            RaisePropertyChanged(nameof(MeterLatestData));
        }
    }
}
